/**
 * @Title: LotteryController
 * @Package cn.com.yiqi.wsc.api.controller
 * @Description: TODO
 * Copyright: Copyright (c) 2016
 * Company: 成都壹柒互动科技有限公司
 * @author 会员系统组-林威
 * @date 2017/2/20 15:33
 * @version V1.0
 */
package cn.com.yiqi.wsc.api.controller;

import cn.com.yiqi.wsc.Ro.LotterRecordVO;
import cn.com.yiqi.wsc.Ro.LotterVO;
import cn.com.yiqi.wsc.api.controller.ro.CommonRO;
import cn.com.yiqi.wsc.common.Result;
import cn.com.yiqi.wsc.common.ResultUtil;

import cn.com.yiqi.wsc.entity.lottery.LotteryActivity;
import cn.com.yiqi.wsc.service.base.BaseParaService;
import cn.com.yiqi.wsc.service.customer.CustomerService;
import cn.com.yiqi.wsc.service.file.dataobject.QiniuConfig;
import cn.com.yiqi.wsc.service.lottery.impl.LotterServiceImpl;

import java.util.ArrayList;
import java.util.List;

import cn.com.yiqi.wsc.service.lottery.impl.LotteryRecordImpl;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 *
 *@ClassName: LotteryController
 * Description: TODO
 * @author 会员系统组-林威
 * @date 2017/2/20 15:33
 *
 */
@ResponseBody
@Controller
@RequestMapping("/api/lottery")
public class ApiLotteryController {
    @Autowired
    LotterServiceImpl lotterService;

    @Autowired
    LotteryRecordImpl recordService;

    @Autowired
    CustomerService customerService;

    @Autowired
    QiniuConfig qiniuConfig;

    @Autowired
    BaseParaService baseParaService;
    /**
     * 活动图片
     * */
    @RequestMapping("/preIndex")
    public Result indexPre(){
        Result re = new Result();
        List<LotteryActivity> activitys = lotterService.findActivity();
        if(null != activitys){
            for (int i = 0; i < activitys.size(); i++) {
                activitys.get(i).setBanner(qiniuConfig.getURL()+"/"+activitys.get(i).getBanner());
            }
        }
        re.putData("activitys",activitys);
        re.putData("signLogo",qiniuConfig.getURL()+"/"+baseParaService.getByCode("SIGNED_LOGO"));
        return re;
    }


    /**
     * 大转盘首页
     * */
    @RequestMapping("/index")
    public Result index(@RequestBody CommonRO ro){
        Result re = new Result();
        //参与抽奖的奖品
        if(null == ro.getActivityId()){
            return ResultUtil.fail(re,"param error");
        }
        LotteryActivity activity = lotterService.findActivityById(ro.getActivityId());
        if(activity ==null){
            return ResultUtil.fail(re,"没有抽奖活动");
        }
        List<LotterVO> lotters = lotterService.findLotters(activity.getId(),qiniuConfig.getURL()+"/");
        if(lotters != null){
            //插入按钮项目
            Integer size = lotters.size();
            while (size < 8){
                LotterVO vo = new LotterVO();
                lotters.add(vo);
                size++;
            }
            LotterVO vo = new LotterVO();
            vo.setType(-1);
            lotters.add(4,vo);
            re.putData("lotters",lotters);
        }

        //中奖记录
        List<LotterRecordVO> lotterRecordVOs = recordService.findPrizes(30,activity.getId());
        for (LotterRecordVO vo :lotterRecordVOs){
            if(StringUtils.isNotEmpty(vo.getMobile())){
                vo.setMobile(vo.getMobile().substring(0,3)+"****"+vo.getMobile().substring(7,vo.getMobile().length()));
            }
        }
        int size = lotterRecordVOs.size();
        if(size > 0){
            int groups = size % 3 == 0 ? size/3 : (size/3) + 1;
            for (int i = 0; i < groups; i++) {
                re.putData("lotteryRecords"+(i+1),lotterRecordVOs.subList(i*3,(i+1) * 3 > size ? size : (i+1)*3));
            }
        }
        re.putData("url",qiniuConfig.getURL()+"/"+activity.getBanner());
        re.putData("memo",activity.getMemo());
        re.putData("name",activity.getName());
        return re;
    }

//    /**
//     * 中奖记录 分页
//     * */
//    @RequestMapping("/lotteryRecord")
//    public Result lotteryRecord(@RequestBody CommonRO ro){
//        Result re = new Result();
//        if(null == ro.getActivityId()){
//            return ResultUtil.fail(re,"param error");
//        }
//        if(null == ro.getCurrentPage()){
//            ro.setCurrentPage(1);
//            ro.setPageSize(3);
//        }
//        //中奖记录
//        PageInfo<LotterRecordVO> page = recordService.findPrizes(ro.getCurrentPage(),ro.getPageSize(),ro.getActivityId());
//        for (LotterRecordVO vo :page.getList()){
//            if(StringUtils.isNotEmpty(vo.getMobile())){
//                vo.setMobile(vo.getMobile().substring(0,3)+"****"+vo.getMobile().substring(7,vo.getMobile().length()));
//            }
//        }
//        re.putData("page",page);
//      return re;
//    }

    /**
     * 大转盘抽奖
     * */
    @RequestMapping("/draw")
    public Result draw(@RequestBody CommonRO ro){
        Result re;
        re = customerService.isExist(ro==null?null:ro.getUserId());
        if(!ResultUtil.isSuccess(re) || null == ro.getActivityId()){
            return ResultUtil.fail(re,"参数错误！");
        }
        //抽奖
        re = recordService.draw(ro.getUserId(),ro.getActivityId());
        return re;
    }

    /**
     * 大转盘积分兑换次数
     * */
    @RequestMapping("/buyTime")
    public Result buyTime(@RequestBody CommonRO ro){
        Result re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re) || null == ro.getActivityId()){
            return ResultUtil.fail(re,"param error");
        }
        re = recordService.buyTime(ro.getUserId(),ro.getActivityId());
        return re;
    }

    /**
     * 奖品记录
     *
     */
    @RequestMapping("/record")
    public Result record(@RequestBody CommonRO ro){
        Result re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        if(null == ro.getPageSize() ){
            ro.setPageSize(10);
        }
        if(null == ro.getCurrentPage()){
            ro.setCurrentPage(1);
        }

        PageInfo<LotterRecordVO> page = recordService.lotteryrecord(ro.getUserId(),ro.getCurrentPage(),ro.getPageSize());
        re.putData("list",page);
        return re;
    }

    /**
     * 领奖
     * */
    @RequestMapping("/getLottery")
    public Result get(@RequestBody CommonRO ro){
        Result re = customerService.isExistUserAndAddress(ro.getUserId(),ro.getAddressId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        // 领奖
        re = recordService.getLottery(ro.getId(),ro.getUserId(),ro.getAddressId());
        return re;
    }

    /**
     * 分享增加抽奖次数
     * */
    @RequestMapping("/addTicket")
    public Result addTicket(@RequestBody CommonRO ro){
        Result re = customerService.isExist(ro.getUserId());
        if(!ResultUtil.isSuccess(re)){
            return re;
        }
        re = recordService.addTikect(ro.getUserId(),ro.getActivityId());
        return re;
    }

}
